package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.RedPacketBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.flyco.dialog.widget.base.BaseDialog;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/22.
 */

public class RedPackectDialog extends BaseDialog {
    @BindView(R.id.image_redpacket)
    ImageView imageRedpacket;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.ll_res)
    LinearLayout llRes;
    @BindView(R.id.image_finish)
    ImageView imageFinish;
    @BindView(R.id.tv_tuiguang)
    TextView tvTuiguang;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    private Context context;
    private MyProgressLoading loadingDialog;
    private String id;

    public RedPackectDialog(Context context, String id) {
        super(context);
        this.context = context;
        this.id = id;
    }


    @Override
    public View onCreateView() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_red_packet, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void setUiBeforShow() {
//        if (TextUtils.equals(id, Constant.RED_PACKET_FIRST)) {
//            tvConfirm.setText("20.00元");
//        } else {
            tvConfirm.setText("点击领取红包哟！");
//        }
        loadingDialog = new MyProgressLoading(context, R.style.DialogStyle);
    }

    @OnClick({R.id.tv_confirm, R.id.image_finish,R.id.image_redpacket})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                if (TextUtils.equals(id, Constant.RED_PACKET_FIRST)) {return;}
                getRedPacket();
                break;
            case R.id.image_redpacket:
                if (TextUtils.equals(id, Constant.RED_PACKET_FIRST)) {return;}
                if(tvConfirm.isShown()){
                    getRedPacket();
                }
                break;
            case R.id.image_finish:
                dismiss();
                break;
        }
    }

    private void getRedPacket() {
        loadingDialog.show();
        OkgoUtil.post(Http.ReceiveRedBag, context)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        RedPacketBean parentBean = DealResult.getInstance().dealDatas(context, response, new TypeToken<RedPacketBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            ToastUtils.toastForShort(context, parentBean.getResult());
                            imageRedpacket.setBackgroundResource(R.mipmap.icon_hongbaob);
                            llRes.setVisibility(View.VISIBLE);
                            tvConfirm.setVisibility(View.GONE);
                            RedPacketBean.BodyBean bean = parentBean.getBody();
                            if (bean != null) {
                                tvMoney.setText(context.getString(R.string.rmb) + bean.getPrice());
                            }
                        } else if (parentBean.getCode() == Constant.ERROR) {
                            RedPackectDialog.this.dismiss();
                        }
                        ToastUtils.toastForShort(context, parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                    }
                });
    }


}
